package vn.fpt.snake.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;

import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.controller.GameControllerImpl;
import vn.fpt.snake.entity.Settings;
import vn.fpt.snake.view.Board;

public class StartupViewImpl implements IView {
	private Board parent;
	private GameControllerImpl gameController;
	//Store menu game <Menu, PointOfMenu>
	private HashMap<String, Point> menu; 
	List<String> choices;
	//Store choice of menu
	String choice; 
	Clip choiceClip;
	AudioInputStream audioInput;

	public StartupViewImpl(Board parent) throws Exception{
		this.parent = parent;
		init();	
	}
	
	public void init() throws Exception{		
		gameController = new GameControllerImpl();
		choices = new ArrayList<String>();
		choices.add(Constants.LOAD_GAME);
		choices.add(Constants.NEW_GAME);
		choices.add(Constants.OPTIONS);
		choices.add(Constants.HIGH_SCORES);	
		choices.add(Constants.INSTRUCTION);
		choices.add(Constants.EXIT_GAME);
		choice = choices.get(0);
	}
	
	@Override
	public void onPaint(Graphics g) throws Exception{
		// TODO Auto-generated method stub
		//draw background
		g.drawImage(gameController.getImage(Constants.START_BG_IMG), 0, 0, parent.getWidth(), parent.getHeight(), parent);
		//draw menu
		drawMenu(g);
	}
	
	public void drawMenu(Graphics g) throws Exception{	
		Settings settings = gameController.getSettings();
		int dotSize = settings.getDotSize();
		int width = parent.width;
		int height = parent.height;
		int widthSnake = width / 5;
		int heightSnake = height / 4 - dotSize;	
		int xSnake = widthSnake * 2;
		int ySnake = heightSnake;

		Font font = new Font("Cooper Black", Font.BOLD, dotSize);
		FontMetrics metrics = g.getFontMetrics(font);
	
		int heightOfText = metrics.getHeight() + dotSize;
		int top = heightSnake * 2 + dotSize * 2; 
		
		if(menu == null){
			menu = new HashMap<>();
			for(int i = 0; i < choices.size(); i++){
				String optionText = choices.get(i);
				int widthOfOption = (width - metrics.stringWidth(optionText)) / 2;
				Point pointOfOption = new Point(widthOfOption, top + heightOfText * i);
				menu.put(optionText, pointOfOption);		
			}

		}
		
		g.drawImage(gameController.getImage(Constants.SNAKE_IMG), xSnake, ySnake, widthSnake, heightSnake, parent);
		g.setFont(font);
				
 		for(Object key : menu.keySet()){
 			g.setColor(new Color(89, 12, 218));
 			if(key.toString().equals(choice))
 				g.setColor(Color.RED);
 			Point point = menu.get(key);
 			g.drawString(key.toString(), point.x, point.y); 
 		}
		
	}

	@Override
	public void keyPressed(KeyEvent arg) {
		// TODO Auto-generated method stub
		try{
			int keyCode = arg.getKeyCode();
			if(parent._view instanceof StartupViewImpl){
				int choiceSize = choices.size() - 1;
				int index = choices.indexOf(choice);
				if(keyCode == KeyEvent.VK_W){									
					if(index == 0){
						choice = choices.get(choiceSize);
					}else{
						choice = choices.get(index-1);
					}
				}else if(keyCode == KeyEvent.VK_S){
					if(index == choiceSize){
						choice = choices.get(0);
					}else{
						choice = choices.get(index+1);
					}
				}else if(keyCode == KeyEvent.VK_ENTER){
					if(choice == Constants.LOAD_GAME){
						JOptionPane.showMessageDialog(parent, "Function is building!!!", "WARNING", JOptionPane.WARNING_MESSAGE);
					}if(choice == Constants.OPTIONS){
						JOptionPane.showMessageDialog(parent, "Function is building!!!", "WARNING", JOptionPane.WARNING_MESSAGE);
					}if(choice == Constants.NEW_GAME){
						gameController.switchNewGameView(parent);
					}if(choice == Constants.HIGH_SCORES){
						gameController.switchHighScoreView(parent);
					}if(choice == Constants.INSTRUCTION){
						gameController.switchInstructionView(parent);
					}if(choice == Constants.EXIT_GAME){
						gameController.exitGame();
					}
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}
